package com.shanzhu.flower.controller;

import com.shanzhu.flower.entity.Announcement;
import com.shanzhu.flower.entity.R;
import com.shanzhu.flower.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告Controller
 */
@RestController
@RequestMapping("/announcement")
@CrossOrigin
public class AnnouncementController {
    
    @Autowired
    private AnnouncementService announcementService;
    
    /**
     * 获取所有启用的公告（用户端）
     */
    @GetMapping("/list")
    public R getEnabledAnnouncements() {
        R r = new R();
        try {
            List<Announcement> announcements = announcementService.getEnabledAnnouncements();
            return r.setCode(2000).setData(announcements);
        } catch (Exception e) {
            return r.setCode(4000).setMsg("获取公告列表失败");
        }
    }
    
    /**
     * 获取所有公告（管理员端）
     */
    @GetMapping("/admin/list")
    public R getAllAnnouncements() {
        R r = new R();
        try {
            List<Announcement> announcements = announcementService.getAllAnnouncements();
            return r.setCode(2000).setData(announcements);
        } catch (Exception e) {
            return r.setCode(4000).setMsg("获取公告列表失败");
        }
    }
    
    /**
     * 根据ID获取公告详情
     */
    @GetMapping("/{id}")
    public R getAnnouncementById(@PathVariable Integer id) {
        R r = new R();
        try {
            Announcement announcement = announcementService.getById(id);
            if (announcement != null) {
                return r.setCode(2000).setData(announcement);
            } else {
                return r.setCode(4000).setMsg("公告不存在");
            }
        } catch (Exception e) {
            return r.setCode(4000).setMsg("获取公告详情失败");
        }
    }
    
    /**
     * 新增公告
     */
    @PostMapping("/add")
    public R addAnnouncement(@RequestBody Announcement announcement) {
        R r = new R();
        try {
            if (announcement.getTitle() == null || announcement.getTitle().trim().isEmpty()) {
                return r.setCode(4000).setMsg("公告标题不能为空");
            }
            if (announcement.getContent() == null || announcement.getContent().trim().isEmpty()) {
                return r.setCode(4000).setMsg("公告内容不能为空");
            }
            
            boolean success = announcementService.addAnnouncement(announcement);
            if (success) {
                return r.setCode(2000).setMsg("公告发布成功");
            } else {
                return r.setCode(4000).setMsg("公告发布失败");
            }
        } catch (Exception e) {
            return r.setCode(4000).setMsg("公告发布失败");
        }
    }
    
    /**
     * 更新公告
     */
    @PostMapping("/update")
    public R updateAnnouncement(@RequestBody Announcement announcement) {
        R r = new R();
        try {
            if (announcement.getId() == null) {
                return r.setCode(4000).setMsg("公告ID不能为空");
            }
            if (announcement.getTitle() == null || announcement.getTitle().trim().isEmpty()) {
                return r.setCode(4000).setMsg("公告标题不能为空");
            }
            if (announcement.getContent() == null || announcement.getContent().trim().isEmpty()) {
                return r.setCode(4000).setMsg("公告内容不能为空");
            }
            
            boolean success = announcementService.updateAnnouncement(announcement);
            if (success) {
                return r.setCode(2000).setMsg("公告更新成功");
            } else {
                return r.setCode(4000).setMsg("公告更新失败");
            }
        } catch (Exception e) {
            return r.setCode(4000).setMsg("公告更新失败");
        }
    }
    
    /**
     * 删除公告
     */
    @PostMapping("/delete/{id}")
    public R deleteAnnouncement(@PathVariable Integer id) {
        R r = new R();
        try {
            boolean success = announcementService.deleteAnnouncement(id);
            if (success) {
                return r.setCode(2000).setMsg("公告删除成功");
            } else {
                return r.setCode(4000).setMsg("公告删除失败");
            }
        } catch (Exception e) {
            return r.setCode(4000).setMsg("公告删除失败");
        }
    }
    
    /**
     * 更新公告状态
     */
    @PostMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        R r = new R();
        try {
            boolean success = announcementService.updateStatus(id, status);
            if (success) {
                return r.setCode(2000).setMsg("状态更新成功");
            } else {
                return r.setCode(4000).setMsg("状态更新失败");
            }
        } catch (Exception e) {
            return r.setCode(4000).setMsg("状态更新失败");
        }
    }
} 